import { AdConfigData } from "../ShiyouAdManager";
import { ui } from "../../../../../ui/layaMaxUI";
import { EventTool } from "../../../Tool/EventTool";
import GameAdsManager from "../../GameAdsManager";
import MathTool from "../../../Tool/MathTool";


export default class AdItem extends ui.ShiyouAdView.Items.AdItemUI {
    
    private m_appid: string = "";
    private m_applabelName: string = "";
    private m_path: string = "";
    private m_appskin: string = "";
    private m_adtype: number = -1;
    private type:string="";
    private order:number=0;

    private m_playingAm:boolean=false;
    private m_pos:Laya.Vector2=new Laya.Vector2();

    OnIconBackClick() {
        if (this.m_appid!="") {
            EventTool.UpLoadUserAdData(this.type,this.order,this.m_appid,"2");
            GameAdsManager.Instance.NativeToProgram(this.m_appid,this.m_applabelName,this.m_path,this.type,this.order);
        }
      
    }


    constructor()  {
        super();
        this.m_IconBack.on(Laya.Event.CLICK, this, this.OnIconBackClick);

    }


    SetData(data: AdConfigData,type:string,order:number,playeam:boolean=false)  
    {
        this.m_appid = data.AppId;
        this.m_applabelName = data.AppName;
        this.m_path=data.Path;
        this.m_appskin = data.AppIcon;
        this.m_appIcon.skin = this.m_appskin;
        this.m_appName.text = this.m_applabelName;
        this.m_adtype = data.AdType;
        this.type=type;
        this.order=order;
        let showhot = data.ShowHot == 1;
        this.m_HotImg.visible = showhot;
        if (playeam&&!this.m_playingAm) {
            this.m_playingAm=true;
            this.ShowRotate();
        }
        EventTool.UpLoadUserAdData(this.type,this.order,data.AppId,"1");
    }


    public ShowRotate(timer = 1500, minAngle = -10, maxAngle = 10)  {
        
        this.RotateTo(timer, minAngle, maxAngle);
    }

   


    ShowPosAm(){
        this.m_pos.x=this.x;
        this.m_pos.y=this.y;
        this.pos(500,2000);
        this.rotation=MathTool.GetRandomInt(1800,3600);
        let timer=MathTool.GetRandomInt(700,1500)
        Laya.Tween.to(this, { x: this.m_pos.x,y:this.m_pos.y }, timer);
        Laya.Tween.to(this, { rotation: 0}, timer);
    }

    RotateTo(timer, minAngle, maxAngle)  {
        Laya.Tween.to(this.m_IconBack, { rotation: maxAngle }, timer, Laya.Ease.linearNone, Laya.Handler.create(this, this.RotateBack, [timer, minAngle, maxAngle]));
    }
    RotateBack(timer, minAngle, maxAngle)  {
        Laya.Tween.to(this.m_IconBack, { rotation: minAngle }, timer, Laya.Ease.linearNone, Laya.Handler.create(this, this.RotateTo, [timer, minAngle, maxAngle]));
    }
}